﻿using Abp.Dependency;
using Abp.UI;
using ARchGL.Platform.Web.Configuration;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Text;

namespace ARchGL.Platform.Web.Validation
{
    public class JN_ValidationFilter : IActionFilter, ITransientDependency
    {
        private readonly IAbpConfiguration _configuration;
        private readonly IIocResolver _iocResolver;

        public JN_ValidationFilter(IIocResolver iocResolver, IAbpConfiguration configuration)
        {
            _iocResolver = iocResolver;
            _configuration = configuration;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //throw new UserFriendlyException("JN_ValidationFilter Throw");
            if (!_configuration.IsValidationEnabledForControllers)
            {
                return;
            }

            if (filterContext.ActionDescriptor.Id == null)
            {
                return;
            }
            //var methodInfo = filterContext.ActionDescriptor.GetMethodInfoOrNull();
            //if (methodInfo == null)
            //{
            //    return;
            //}

            using (var validator = _iocResolver.ResolveAsDisposable<AbpActionInvocationValidator>())
            {
                //validator.Object.Initialize(filterContext, methodInfo);
                validator.Object.Validate();
            }
        }
    }
}
